package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.SuppMaterial.SuppMaterialService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.Material;
import com.bdqn.entity.SuppMaterial;
import com.bdqn.entity.Supplier;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/SuppMaterial")
public class SuppMaterialController {
    @Autowired
    private SuppMaterialService suppMaterialService;
    @Autowired
    private SupplierService supplierService;
    /**
     * 根据当前登录的id查询供应商的产品
     * @return
     */
    @RequestMapping("/SuppMaterial.html")
    @ResponseBody
    public Layui<SuppMaterial> selectSuppMateriallist(Material  m, String orderby, Integer page, Integer limit){
//        Subject subject= SecurityUtils.getSubject();
//        Employee employee =(Employee) subject.getSession().getAttribute("employee");//员工
        Subject subject= SecurityUtils.getSubject();
        Employee employee =(Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());//根据id查询供货商
        //判断当前页是否为空如果为空那就为第一页
        if(page==null||page.equals("")){
            page=1;
        }
        //获得sql里面的
        if(limit==null||limit.equals("")){
            limit=10;
        }
        if(orderby==null||orderby.equals("")){
            orderby="m.ID";
        }
        page= (page - 1) * limit;
      //获取查询方法 条件和分页
        List<SuppMaterial> suppMaterials = suppMaterialService.selectSuppMaterialBy(supplier.getUserId(), m.getMaterialNum(), m.getMaterialName(), orderby, page, limit);
        Layui<SuppMaterial> suppMaterialLayui = new Layui<>();
        suppMaterialLayui.setCode(0);
        suppMaterialLayui.setData(suppMaterials);
        //获取总条数
        int count = suppMaterialService.SuppMaterialCountBy(supplier.getUserId(), m.getMaterialNum(), m.getMaterialName());
        suppMaterialLayui.setCount(count);//总数量，查
        return suppMaterialLayui;
    }
    /**
     * 根据id 删除 对应供应商的值
     * @return
     */
    @RequestMapping("/deleteSuppMaterial.html")
    @ResponseBody
   public int deleteSuppMaterialby(Integer mid){
        Subject subject= SecurityUtils.getSubject();
        Employee employea =(Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employea.getId());
        int de=suppMaterialService.SuppMaterialDeleteBy(supplier.getId(),mid);
       return de;
   }


    /**
     * 添加功能的完成
     */
    @RequestMapping("/addSuppMaterialOK.html")
    @ResponseBody
    public  int   addSuppMaterial(Integer mid){
        //添加操作
        Subject subject= SecurityUtils.getSubject();
        Employee employea =(Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employea.getId());
        int sid=supplier.getId();
        //添加判断这个用户是否存
        SuppMaterial suppMaterial = suppMaterialService.selectSuppMaterialByIds(sid, mid);
        System.out.println("aaaaaaaaaaaa"+suppMaterial);
        if(suppMaterial==null){
            int de=suppMaterialService.addSuppMaterial(sid,mid);
            return de;
        }
        else{
            return 3;
        }

    }
}
